package
{
	import adobe.utils.CustomActions;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.graphics.Stroke;
	import com.smartfoxserver.v2.entities.variables.SFSRoomVariable;
	
	import Team;
	
	/**
	 * ...
	 * @author Bryan Keiren & Sander van Katwijk
	 */
	// When captured, will create units for the owner.
	public class Flag
	{
		static protected var m_FlagList:Vector.<Flag> = new Vector.<Flag>;
		static protected function registerFlag(a_Flag:Flag):void	{	m_FlagList.push(a_Flag); 							/*Engine.printf("Number of flags: " + m_FlagList.length);*/	}
		static protected function unRegisterFlag(a_Flag:Flag):void	{	m_FlagList.splice(m_FlagList.indexOf(a_Flag), 1); 	/*Engine.printf("Number of flags: " + m_FlagList.length);*/	}
		static public function getFlags():Vector.<Flag>				{	return m_FlagList;								}
		static public function getFlagByID(a_ID:int):Flag			{	for each(var flag:Flag in m_FlagList)	
																			if (flag.ID == a_ID)	return flag;
																		return null;									}
		
		public var ID:int;
		
		private var m_Pos:Vector2;
		private var m_GridPos:Vector2;
		private var m_Team:Team = Team.getDefaultTeam();
		public function getTeam():Team	{	return m_Team;	}
		public function attemptCapture( a_Team:Team, force:Boolean = false, a_SendToServer:Boolean = false ):void
		{
			if (a_Team.getID() == m_Team.getID())
				return;
			if ( !force && !(m_Team == null) && !(m_Team.getID() == Team.getDefaultTeam().getID()) )
			{
				for each(var player:Player in m_Team.getPlayers())
				{
					if (player.getPos().distSq(m_Pos) < Globals.Flag_Capture_RadiusSq)
					{
						Engine.printf("Players in range, no cap");
						return;
					}
				}
			}
			//Engine.printf("Setting flag team to " + a_Team.ID);
			if (!a_Team) a_Team = Team.getDefaultTeam();
			m_Team = a_Team;
			m_IsoBox.stroke = a_Team.getStroke();
			
			if (a_SendToServer)
			{
				Engine.printf("Sending capture to server");
				var settings:Array = [];
				settings.push( new SFSRoomVariable("node" + ID + "owner", a_Team.ID) );
				MessageHandler.Get().sendSetRoomVariablesRequest(settings, Globals.Current_Game_Room);
			}
		}
		
		private var m_IsoBox:IsoBox;
		
		private var m_ScoreTimer:int = 0;			// ms.
		private var m_ScoreInterval:int = 1000;		// ms.
		private var m_MoveTimer:int = 0;			// ms.
		private var m_MoveInterval:int = 6000;	// ms.
		
		public function Flag(a_GridPos:Vector2, a_ID:int) 
		{
			
			ID = a_ID;
			registerFlag(this);
			//Engine.printf("Creating flag " + ID);
			m_GridPos = a_GridPos;
			m_Pos = a_GridPos.multiplyN(Globals.Cell_Size).addV(new Vector2(Globals.Cell_Size / 2, Globals.Cell_Size / 2));
			
			m_IsoBox = new IsoBox();
			m_IsoBox.setSize(Globals.Cell_Size/5, Globals.Cell_Size/5, 50);
			m_IsoBox.moveTo(m_Pos.x - m_IsoBox.width,
							m_Pos.y - m_IsoBox.length,
							0);
			
			m_IsoBox.container.mouseEnabled = false;
			
			attemptCapture(Team.getDefaultTeam(), true);
			
			Game.Get().getScene().addChild(m_IsoBox);
		}
		public function destroy():void
		{
			//Engine.printf("Deleting flag " + ID);
			unRegisterFlag(this);
			delete this;
		}
		
		static public function destroyAllFlags():void
		{
			while(m_FlagList.length)
				m_FlagList[0].destroy();
		}
		
		public function setX(a_X:int):void
		{
			m_GridPos.x = a_X;
			m_Pos = m_GridPos.multiplyN(Globals.Cell_Size).addV(new Vector2(Globals.Cell_Size / 2, Globals.Cell_Size / 2));
			
			m_IsoBox.moveTo(m_Pos.x - m_IsoBox.width,
							m_Pos.y - m_IsoBox.length,
							0);
		}
		
		public function setY(a_Y:int):void
		{
			m_GridPos.y = a_Y;
			m_Pos = m_GridPos.multiplyN(Globals.Cell_Size).addV(new Vector2(Globals.Cell_Size / 2, Globals.Cell_Size / 2));
			
			m_IsoBox.moveTo(m_Pos.x - m_IsoBox.width,
							m_Pos.y - m_IsoBox.length,
							0);
		}
		
		public function logic():void
		{
			if (m_Team == null)
				return;
			
			/*
			if (isSecured() && m_Team.getID() == LocalPlayer.Get().getTeam().getID())
			{
				m_MoveTimer += Globals.Delta_Time_MS;
				if (m_MoveTimer >= m_MoveInterval)
				{
					m_MoveTimer = 0;
					
					//Engine.printf("# " + ID + " is going to a different spot");
					
					// Move to a different spot.
					setX(Math.random() * Globals.Grid_Size);
					setY(Math.random() * Globals.Grid_Size);
					
					var newPosArray:Array = [];
					newPosArray.push( new SFSRoomVariable("node" + ID + "x", m_GridPos.x) );
					newPosArray.push( new SFSRoomVariable("node" + ID + "y", m_GridPos.y) );
					MessageHandler.Get().sendSetRoomVariablesRequest(newPosArray, Globals.Current_Game_Room);
				}
			}
			else
			{
				m_MoveTimer = 0;
			}
			*/
		}
		
		public function getPos():Vector2
		{
			return m_Pos;
		}
	}
}